#include <stdio.h>
#include <thread>
#include <math.h>

#define SHM_INI_IMPLEMENTATION
#define SHM_LOG_IMPLEMENTATION
#define SHM_QUEUE_IMPLEMENTATION

#include "shm_log.h"

bool running = true;
const char *log_name = "test_log";

int main() {
  shm_queue_t logqueue;
  //shm_log_init2(log_name, 1024, 1024, LOG_DEBUG);

  shm_log_init("log_service.ini", "test");

  log_info("test %d", 12345);

  {
    shm_queue_options_t options;
    options.elem_count = 1024;
    options.elem_size = 1024;
    options.mode = SHM_QUEUE_MODE_READ | SHM_QUEUE_MODE_CREATE;
    options.name = log_name;
    int rslt = shm_queue_open(&logqueue, &options);

  }

  uint32_t log_count = 0;
  std::thread thread([&log_count]() {
    while (running) {
      log_info("count:%d", ++log_count);
      //printf("shm_queue_write ret:%d\n", rslt);
      std::this_thread::sleep_for(std::chrono::milliseconds(1));
    }
  });

  char buffer[1024];
  uint32_t count = 0;
  while (1) {
    int rslt = shm_queue_read(&logqueue, buffer, sizeof(buffer));
    if (rslt > 0) {
      buffer[rslt] = '\0';
      printf("%s\n", buffer);
    } else {
      std::this_thread::sleep_for(std::chrono::milliseconds(1));
    }
  }

  running = false;
  thread.join();
  printf("finish\n");
  shm_queue_close(&logqueue);
  shm_log_destroy();
  return 0;
}
